#'
#' @title 免疫浸润组合出图
#' @description 鉴于方案中很多组合出图，依据已有function以及结果，组合出一组图，临时使用Function
#' @param immune_score_res_dir 免疫浸润RData结果路径
#' @param Feature_in_ESTIMATE estimate中展示部分,可选I、E、S、T，或者全称，代表"ImmuneScore", "ESTIMATEScore", "StromalScore", "TumorPurity"
#' @param Feature_choose 哪种算法展示，可选c、ep、x、es、s，或者全称，代表"cibersort", "epic", "xcell", "estimate", "ssgsea"
#' @param group_df 分组信息，第一列为`sample`，第二列为`Score`，第三列为`Group`，必须包这三列，对应样本、样本得分、样本分组信息
#' @param od 结果输出路径
#' @param w 图宽
#' @param h 图高
#' @export
#' @return ggplot对象
#' @author *WYK*
#'
Immune_Plot_Combine <- function(immune_score_res_dir,
                                Feature_in_ESTIMATE = c("I", "E"),
                                Feature_choose = "ciber",
                                group_df = NULL, od, w = 11.2, h = 8, saveFile = T) {
    if (!missing(immune_score_res_dir)) {
        tmp <- load(immune_score_res_dir)
        assign("immune_score_res", get0(tmp))
    }

    Feature_in_ESTIMATE_selected <- match.arg(
        arg = Feature_in_ESTIMATE,
        choices = c("ImmuneScore", "ESTIMATEScore", "StromalScore", "TumorPurity"), several.ok = T
    )
    Feature_choose_selected <- match.arg(
        arg = Feature_choose,
        choices = c("cibersort", "epic", "xcell", "estimate", "ssgsea"), several.ok = F
    )

    immu_res <- Immune_infiltration(
        immucell_res = immune_score_res[[Feature_choose_selected]] %>% as.data.frame(), 
        saveplot = F, 
        risk_infor = group_df %>% rename(riskgroup = Group)
    )

    dat <- immune_score_res$estimate %>%
        inner_join(group_df %>% select(sample, Group))

    library(ggpubr)
    p_list_box <- map(Feature_in_ESTIMATE_selected, function(type) {
        # type <- "ImmuneScore"
        p <- dat %>%
            ggplot(aes_string(x = "Group", y = type, fill = "Group")) +
            geom_boxplot(size = .6, width = .5, outlier.shape = NA) +
            geom_jitter(width = .2, size = .6) +
            stat_compare_means() +
            theme_pubr() +
            scale_fill_manual(values = RColorBrewer::brewer.pal(6, "Set1")) +
            theme(legend.position = "none")
        return(p)
    })

    p_box <- ggarrange(plotlist = p_list_box, align = "hv", labels = "AUTO")

    dat2 <- immune_score_res$estimate %>%
        select(sample, any_of(Feature_in_ESTIMATE_selected)) %>%
        inner_join(group_df %>% select(sample,Score))

    p_list_cor <- map(Feature_in_ESTIMATE_selected, function(type) {
        p <- ggscatter(dat2,
            x = "Score", y = type, size = .55, alpha = .8,
            add = "reg.line", # Add regressin line
            add.params = list(color = "#0000ffa8", fill = "lightgray"),
        ) +
            stat_cor()
        return(p)
    })

    p_cor <- ggarrange(plotlist = p_list_cor, common.legend = T, align = "hv", labels = c("C", "D"))

    p_all <- ggarrange(ggarrange(p_box, p_cor, align = "h"),
        immu_res$figure_immune_infiltration,
        nrow = 2, align = "none", common.legend = T, heights = c(.7, 1),
        labels = c("", "E")
    )

    if (isTRUE(saveFile)) {
        plotout(
            p = p_all,
            od = od,
            name = "immu_part",
            w = w,
            h = h
        )
    }

    return(p_all)
}
